﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiSanThem : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormTaiSanThem()
        {
            InitializeComponent();
        }
     
        private void bttLuu_Click(object sender, EventArgs e)
        {
            Create();
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormTaiSanThem_Load(object sender, EventArgs e)
        {
            bttLuu.Enabled = false;
            GetNhomTaiSan();
            GetListDonViTinh();
            dtiThoiGianBatDauSuDung.Value = DateTime.Now;
        }
        private void txtThoiGianKhauHao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtNamSanXuat_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtTiLeKhauHao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        void Create()
        {
            try
            {
                var t = new TaiSan();
                t.TenTaiSan = txtTenTaiSan.Text;
                t.NhomTaiSanId = Convert.ToInt32(cbbNhomTaiSan.SelectedValue);
                t.DonViTinhId = Convert.ToInt32(cbbDonViTinh.SelectedValue);
                t.NamSanXuat = txtNamSanXuat.Text;
                t.ThoiGianBatDauSuDung = dtiThoiGianBatDauSuDung.Value;
                t.ThoiGianKhauHao = Convert.ToInt32(txtThoiGianKhauHao.Text);
                t.GiaTriKhauHao = 0;
                t.TiLeKhauHao = 1;
                t.DienGiai = txtDienGiai.Text;
                t.NgayTinhKhauHao = DateTime.Now;
                t.TrangThai = 0;
                db.TaiSans.Add(t);
                db.SaveChanges();
                Reset();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

        void GetNhomTaiSan()
        {
            cbbNhomTaiSan.DataSource = db.NhomTaiSans.ToList();
            cbbNhomTaiSan.DisplayMember = "TenNhomTaiSan";
            cbbNhomTaiSan.ValueMember = "Id";
        }

        void GetListDonViTinh()
        {
            cbbDonViTinh.DataSource = db.DonViTinhs.ToList();
            cbbDonViTinh.DisplayMember = "TenDonViTinh";
            cbbDonViTinh.ValueMember = "Id";
        }
        void KiemTraDuLieu()
        {
            if (txtTenTaiSan.Text != "" && txtThoiGianKhauHao.Text != "" && Convert.ToInt32(cbbNhomTaiSan.SelectedValue) > 0)
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        void Reset()
        {
            txtDienGiai.Text = "";
            txtNamSanXuat.Text = "";
            txtTenTaiSan.Text = "";
            txtTiLeKhauHao.Text = "";
            dtiThoiGianBatDauSuDung.Value = DateTime.Now;
            txtTenTaiSan.Text = "";
            txtThoiGianKhauHao.Text = "";
            cbbDonViTinh.SelectedValue = 0;
            cbbNhomTaiSan.SelectedValue = 0;
            bttLuu.Enabled = false;
        }

        private void txtTenTaiSan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbNhomTaiSan_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtThoiGianKhauHao_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtTiLeKhauHao_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void dtiThoiGianBatDauSuDung_ValueChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

    }
}
